package com.freedom.auth.domain.service.member;

import com.freedom.auth.infrastructure.enums.AuthenticationIdentityEnum;
import com.freedom.common.core.enums.AuthResultCodeEnum;
import com.freedom.common.core.enums.ums.MemberStatusEnum;
import com.freedom.common.core.result.CommonResult;
import com.freedom.common.framework.exception.ServiceException;
import com.freedom.ums.api.member.feign.MemberFeign;
import com.freedom.ums.api.member.pojo.dto.MemberAuthDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 商城会员用户认证服务
 *
 */
@Service("memberUserDetailsService")
@RequiredArgsConstructor
public class MemberUserDetailsServiceImpl implements UserDetailsService {

    private final MemberFeign memberFeign;

    @Override
    public UserDetails loadUserByUsername(String username) {
        return null;
    }


    /**
     * 会员 - 手机号码认证方式
     *
     * @param mobile 手机号码
     * @return
     */
    public UserDetails loadUserByMobile(String mobile) {
        CommonResult<MemberAuthDTO> result = memberFeign.findMemberByMobile(mobile);
        MemberAuthDTO memberAuth = getMemberAuth(result);
        this.checkMemberStatus(memberAuth);
        return userDetailsBuilder(memberAuth, AuthenticationIdentityEnum.MOBILE);
    }

    public UserDetails loadUserByEmail(String email) {
        CommonResult<MemberAuthDTO> result = memberFeign.findMemberByEmail(email);
        MemberAuthDTO memberAuth = getMemberAuth(result);
        this.checkMemberStatus(memberAuth);
        return userDetailsBuilder(memberAuth, AuthenticationIdentityEnum.EMAIL);
    }

    /**
     * 店铺 - 手机号认证
     * @param mobile 手机号码
     * @return
     */
    public UserDetails loadStoreUserByMobile(String mobile) {
        return null;
    }

    /**
     * openid 认证方式
     *
     * @param openId
     * @return
     */
    public UserDetails loadUserByOpenId(String openId) {
        CommonResult<MemberAuthDTO> result = memberFeign.loadUserByOpenId(openId);
        MemberAuthDTO memberAuth = getMemberAuth(result);
        this.checkMemberStatus(memberAuth);
        return userDetailsBuilder(memberAuth, AuthenticationIdentityEnum.OPENID);
    }

    private UserDetails userDetailsBuilder(MemberAuthDTO member, AuthenticationIdentityEnum authenticationIdentityEnum) {
        MemberUserDetails userDetails = new MemberUserDetails(member, authenticationIdentityEnum);
        userDetails.setAuthenticationIdentity(authenticationIdentityEnum.getValue());
        return userDetails;
    }

    private void checkMemberStatus(MemberAuthDTO member) {
        if (MemberStatusEnum.DISABLED.getValue().equals(member.getStatus())) {
            throw new ServiceException(AuthResultCodeEnum.ACCOUNT_LOCKED);
        } else if (MemberStatusEnum.LOG_OUT.getValue().equals(member.getStatus())) {
            throw new ServiceException(AuthResultCodeEnum.ACCOUNT_LOG_OUT);
        }
    }

    private MemberAuthDTO getMemberAuth(CommonResult<MemberAuthDTO> result) {
        if (!CommonResult.isSuccess(result)) {
            throw new ServiceException(result.getMessage());
        }
        MemberAuthDTO memberAuthDTO = result.getData();
        if (Objects.isNull(memberAuthDTO)) {
            throw new ServiceException(AuthResultCodeEnum.ACCOUNT_NOT_EXIST);
        }
        return memberAuthDTO;
    }

//    private StoreDTO getStore(Integer storeId) {
//        CommonResult<StoreDTO> result = storeFeignClient.loadStoreByStoreId(storeId);
//        if (!CommonResult.isSuccess(result) || Objects.isNull(result.getData())) {
//            throw new UsernameNotFoundException(ResultCode.STORE_NOT_OPEN.getMsg());
//        }
//        return result.getData();
//    }

}
